package com.wechat.back.controller;

import com.wechat.back.anno.OptLog;
import com.wechat.back.pojo.articles.Article;
import com.wechat.back.pojo.tools.PageBean;
import com.wechat.back.pojo.tools.Result;
import com.wechat.back.service.ArticleService;
import com.wechat.back.service.SensitiveWordService;
import com.wechat.back.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static com.wechat.back.utils.SensitiveWordRead.readSensitiveWords;


@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private SensitiveWordService sensitiveWordService;

//    @OptLog(optModule = "文章模块",optType = "添加", optDesc = "添加文章")
    @PostMapping
    public Result add(@RequestBody @Validated(Article.Add.class) Article article) {
        List<String> SensitiveWords = readSensitiveWords();
        String content = article.getContent();
        sensitiveWordService.loadSensitiveWord(SensitiveWords); //加载敏感词
        if (sensitiveWordService.hasSensitiveWord(content)) {
            //包含敏感词
            articleService.addToInspect(article);
            return Result.success("包含敏感词");
        }else {
            //不含敏感词
            articleService.add(article);
            return Result.success();
        }
    }
    @OptLog(optModule = "文章模块",optType = "查询", optDesc = "查看文章")
    @GetMapping("/list")
    public Result<PageBean<Article>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state,
            @RequestParam(required = false) Integer id
    ) {
       PageBean<Article> pb =  articleService.list(pageNum,pageSize,categoryId,state,id);
       return Result.success(pb);
    }

    @GetMapping("/all")
    public Result<PageBean<Article>> listAll(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state
    ) {
        PageBean<Article> pb =  articleService.listAll(pageNum,pageSize,categoryId,state);
        return Result.success(pb);
    }

    @GetMapping("/all-inspect")
    public Result<PageBean<Article>> listAllToInspect(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state
    ) {
        PageBean<Article> pb =  articleService.listAllToInspect(pageNum,pageSize,categoryId,state);
        return Result.success(pb);
    }

    @GetMapping("/all-disallow")
    public Result<PageBean<Article>> listAllToDisAllow(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state
    ) {
        PageBean<Article> pb =  articleService.listAllToDisAllow(pageNum,pageSize,categoryId,state);
        return Result.success(pb);
    }

    @OptLog(optModule = "文章模块",optType = "查询", optDesc = "查看文章详情")
    @GetMapping("/detail")
    public Result<Article> detail(Integer id){
        Article c = articleService.findById(id);
        return Result.success(c);
    }
    //修改已通过文章
    @OptLog(optModule = "文章模块",optType = "修改", optDesc = "修改已通过文章")
    @PutMapping
    public Result update(@RequestBody @Validated(Article.Update.class) Article article) {
        articleService.update(article);
        return Result.success();
    }
    //删除已通过文章
    @OptLog(optModule = "文章模块",optType = "删除", optDesc = "删除已通过文章")
    @DeleteMapping
    public Result delete(Integer id) {
        articleService.deleteById(id);
        return Result.success();
    }
    //删除待审核文章
    @OptLog(optModule = "文章模块",optType = "删除", optDesc = "删除待审核文章")
    @DeleteMapping("/inspect")
    public Result deleteToInspect(Integer id) {
        articleService.deleteToInspectById(id);
        return Result.success();
    }
    //删除已驳回文章
    @OptLog(optModule = "文章模块",optType = "删除", optDesc = "删除已驳回文章")
    @DeleteMapping("/disallow")
    public Result deleteToDisAllow(Integer id) {
        articleService.deleteToDisAllowById(id);
        return Result.success();
    }
    //通过文章：待审核->已通过
    @OptLog(optModule = "文章模块",optType = "修改", optDesc = "修改已通过文章")
    @PutMapping("/allow")
    public Result updateToAllow(@RequestBody @Validated(Article.Update.class) Article article) {
        //将文章添加到已通过
        articleService.add(article);
        //删除待审核中的文章
        int id = article.getId();
        articleService.deleteToInspectById(id);
        return Result.success();
    }
    //驳回文章：待审核->已驳回
    @OptLog(optModule = "文章模块",optType = "修改", optDesc = "修改已驳回文章")
    @PutMapping("/disallow")
    public Result updateToDisAllow(@RequestBody @Validated(Article.Update.class) Article article) {
        //将文章添加到已驳回
        articleService.addToDisAllow(article);
        //删除待审核中的文章
        int id = article.getId();
        articleService.deleteToInspectById(id);
        return Result.success();
    }
    //更改文章状态：已驳回->已通过
    @OptLog(optModule = "文章模块",optType = "修改", optDesc = "修改已驳回文章")
    @PutMapping("/disallow-allow")
    public Result updateDisallowToAllow(@RequestBody @Validated(Article.Update.class) Article article) {
        //将文章添加到已通过
        articleService.add(article);
        //删除已驳回中的文章
        int id = article.getId();
        articleService.deleteToDisAllowById(id);
        return Result.success();
    }
    //更改文章状态：已通过->已驳回
    @OptLog(optModule = "文章模块",optType = "修改", optDesc = "修改通过文章")
    @PutMapping("/allow-disallow")
    public Result updateAllowToDisAllow(@RequestBody @Validated(Article.Update.class) Article article) {
        //将文章添加到已驳回
        articleService.addToDisAllow(article);
        //删除已通过中的文章
        int id = article.getId();
        articleService.deleteById(id);
        return Result.success();
    }
}
